package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.in.business.ebi.InMainEbi;
import cn.hncu.booksManagementSystem.in.business.factory.InMainEbiFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;

public class ListPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private  JFrame mainFrame;
	private JList<InMainModel> JList;
	public static Collection<InMainModel> col	= null;
	public static InDetailQueryModel idqm = null;
	//注入inMainEbi
	InMainEbi inMainEbi = InMainEbiFactory.getInstanceOfInMainEbi();
	/**
	 * Create the panel.
	 */
	public ListPanel( JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
		initJListData();
	}
	
	public ListPanel(JFrame mainFrame, Collection<InMainModel> resCol, InDetailQueryModel idqm) {
		this.mainFrame = mainFrame;
		col = resCol;
		ListPanel.idqm = idqm;
		initContentPane();
		initJListData();
	}
	private void initJListData() {
		if (col==null) {
			JList.setListData(inMainEbi.getAll());
		}else {
			JList.setListData(col.toArray( new InMainModel[0]));
		}
	}
	
	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("进货管理");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(310, 35, 145, 38);
		add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(124, 103, 561, 220);
		add(scrollPane);
		
		JList = new JList<InMainModel>();
		scrollPane.setViewportView(JList);
		JList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount()==2) {
					InMainModel inMain = JList.getSelectedValue();
					if (inMain==null) {
						JOptionPane.showMessageDialog(mainFrame, "请选择您要查看的订单");
						return;
					}
					mainFrame.setContentPane(new DetailPanel(mainFrame, inMain,idqm));
					mainFrame.validate();
				}
			}
		});
		JList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton btnToInPanel = new JButton("填写进货订单");
		btnToInPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new InPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToInPanel.setForeground(Color.BLUE);
		btnToInPanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToInPanel.setBounds(169, 363, 190, 50);
		add(btnToInPanel);
		
		JButton btnToQueryPanel = new JButton("查询...");
		btnToQueryPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new Querypanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToQueryPanel.setForeground(Color.BLUE);
		btnToQueryPanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToQueryPanel.setBounds(465, 363, 128, 50);
		add(btnToQueryPanel);
		
		JLabel lblNewLabel = new JLabel("提示：");
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel.setBounds(476, 50, 61, 27);
		add(lblNewLabel);
		
		JLabel label_1 = new JLabel("双击进货单可以查看进货单明细");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(496, 74, 241, 27);
		add(label_1);
	}
	/**
	 * 初始化 静态属性 col和idqm
	 */
	public static void initStaticAttribute() {
		col = null;
		idqm = null;
	}
}
